package com.linian.pushforcampus.scheduling;

import com.linian.pushforcampus.controller.PushingController;
import com.linian.pushforcampus.controller.WechatController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.net.URISyntaxException;


/**
 * Lennon
 */
@EnableScheduling
@Configuration
@Slf4j
public class Task {


    @Autowired
    private PushingController pushingController;


    @Autowired
    private WechatController wechatController;


    // 按照规定时间请求api，并将json转换成list


    //三秒 @Scheduled(cron = "0/3 * * * * ?")
    //一分钟 @Scheduled(cron = "0 0/1 7-23 * * *")
    @Scheduled(cron = "0 0/1 6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 * * ? ")
    public void jobForPushing() throws URISyntaxException, IOException {

        //删除引擎
        pushingController.getCampusActivityByJson();

    }


    //@Scheduled(cron = "0/3 * * * * ?")
    //每分钟刷新
    @Scheduled(cron = "0 * * * * ?")
    public void jobForRefreshAccessToken() {

        //定时更新微信token
        wechatController.updateWeChatAccessToken();


    }

    //程序启动时将默认执行一次然后再开始进行定时任务
    @PostConstruct
    public void init() throws URISyntaxException, IOException {
        //强制刷新token
        wechatController.forceRefreshAccessToken();
        jobForPushing();

    }


}
